package cn.com.lms.business.livecourse.service;

import cn.com.lms.business.livecourse.persistence.entity.AliLiveStream;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.ParseException;
import java.util.UUID;

/**
 * @author Barry
 * @createDate 2020/5/7
 * @packageName cn.com.lms.business.livecourse.service
 * @description 阿里云直播
 */
@Service
public class AliLiveStreamService {


    private String appName = "bnz-live";
    //播流地址
    private String playHost = "lts.com";
    //推流地址
    private String pushHost = "lts.com";
    //播流访问权健key
    private String playKey = "Oq";
    //推流访问权健key
    private String pushKey = "36";

    public AliLiveStream  getStreams(String streamName, long expireTime) throws ParseException {
//        String rand = "0";
//        String time = dateToStamp(liveStartTime);

        String rand = UUID.randomUUID().toString().replace("-", "");
        System.out.println("过期时间戳：");
        System.out.println(expireTime);
        String strpush = "/" + appName + "/" + streamName + "-" + expireTime + "-" + rand + "-0-" + pushKey;

        AliLiveStream aliLiveStream = new AliLiveStream();
        aliLiveStream.setPushStreamUrl("rtmp://" + pushHost + "/" +appName + "/" + streamName + "?auth_key=" + expireTime + "-" + rand + "-0-" + DigestUtils.md5DigestAsHex(strpush.getBytes()));

        String strpull = "/" + appName + "/" + streamName + "-" + expireTime + "-" + rand + "-0-" + playKey;
        String strpullFlv = "/" + appName + "/" + streamName + ".flv-" + expireTime + "-" + rand + "-0-" + playKey;
        String strpullM3u8 = "/" + appName + "/" + streamName + ".m3u8-" + expireTime + "-" + rand + "-0-" + playKey;
        aliLiveStream.setPullStreamUrl("rtmp://" + playHost + "/" +appName + "/" + streamName + "?auth_key=" + expireTime + "-" + rand + "-0-" + DigestUtils.md5DigestAsHex(strpull.getBytes()));
        aliLiveStream.setPullStreamUrlFlv("http://" + playHost + "/" +appName + "/" + streamName + ".flv?auth_key=" + expireTime + "-" + rand + "-0-" + DigestUtils.md5DigestAsHex(strpullFlv.getBytes()));
        aliLiveStream.setPullStreamUrlM3u8("http://" + playHost + "/" +appName + "/" + streamName + ".m3u8?auth_key=" + expireTime + "-" + rand + "-0-" + DigestUtils.md5DigestAsHex(strpullM3u8.getBytes()));
        aliLiveStream.setExpireTime(expireTime);
        return aliLiveStream;
    }

//    private String dateToStamp() throws ParseException {
//        Long time = System.currentTimeMillis();
//        time = time / 1000;
//        return String.valueOf(time);
//    }
}
